package io.renren.modules.api.controller;

import io.renren.common.exception.RRException;
import io.renren.common.utils.R;
import io.renren.modules.api.annotation.LoginUser;
import io.renren.modules.api.entity.UserEntity;
import io.renren.modules.api.service.IPayService;
import io.renren.modules.psorder.service.PsOrderService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;


/**
 * 支付类的接口集
 * Created by WDJ on 2017/12/15.
 */
@RestController
@RequestMapping("/pay")
public class PayController {

    private Logger logger = Logger.getLogger(PayController.class);

    @Autowired
    private IPayService payService;

    @Autowired
    private PsOrderService psOrderService;

    /**
     * 用户支付配送订单
     * @param user  手机登录用户
     * @param orderNum  配送订单号(唯一)
     * @return
     */
    @PostMapping("aliPayPsOrder")
    public R aliPayPsOrder(@LoginUser UserEntity user,String orderNum,Long couponid){
        String orderString = payService.appAliPayPsOrder(orderNum,couponid);//直接给客户端请求，无需再做处理

        return R.ok().put("orderString",orderString);
    }

    /**
     * 接收支付宝发来的异步通知
     * @throws Exception
     */
    @RequestMapping("aliPayPsOrderNotify")
    public void aliPayPsOrderNotify(HttpServletResponse response, HttpServletRequest request) throws Exception {
        payService.aliPayPsOrderNotify(response,request);
    }

    /**
     * 微信支付配送订单
     * @param user 手机登录用户
     * @param orderNum 配送订单号(唯一)
     * @return
     */
    @PostMapping("wxPayPsOrder")
    public R wxPayPsOrder(@LoginUser UserEntity user,String orderNum,Long couponid,HttpServletRequest request,
                          HttpServletResponse response) {
        Map<String, Object> returnMap;
        try {
            returnMap = payService.appWxPayPsOrderOld(orderNum,couponid,request,response);
        } catch (Exception e) {
            throw new RRException("支付发生异常",-2);
        }

        return R.ok(returnMap);
    }

    /**
     *  微信支付配送订单异步通知处理
     * @param response
     * @param request
     * @throws Exception
     */
    @RequestMapping("wxPayPsOrderNotify")
    public void wxPayPsOrderNotify(HttpServletResponse response, HttpServletRequest request) throws Exception {
        payService.wxPayPsOrderNotifyOld(response,request);
    }






}
